package com.stars.empoly.widget

import android.app.Activity
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import androidx.annotation.DrawableRes
import androidx.databinding.DataBindingUtil
import com.qmuiteam.qmui.kotlin.onClick
import com.stars.empoly.R
import com.stars.empoly.activity.MainActivity
import com.stars.empoly.config.ConfigMMKV
import com.stars.empoly.databinding.WidgetGuideBinding
import com.trailer.base.utils.MMKVManager
import com.trailer.base.utils.UiSwitch

open class WidgetGuide constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : FrameLayout(context, attrs, defStyleAttr) {
    lateinit var binding: WidgetGuideBinding

    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)

    private fun initView(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) {
        binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.widget_guide, this, true)

    }

    private fun listeners() {

    }

    fun setImageResource(@DrawableRes drawableId: Int) {
        binding.imgBg.setImageResource(drawableId)
        if (drawableId == R.drawable.img_bg_n) {
            binding.btnGo.visibility = View.VISIBLE
            binding.btnGo.onClick {

                MMKVManager.instance().encode(ConfigMMKV.FIRST_OPEN, false)
                UiSwitch.single(context, MainActivity::class.java)

                (context as Activity).finish()
            }
        } else {
            binding.btnGo.visibility = View.GONE
        }
    }

    init {
        initView(context, attrs, defStyleAttr)
        listeners()
    }
}
